<!--

业主代表编制进度检查计划

-->
<template>
  <my-dialog @on-submit="submitHandle" :subLoading="subLoading">
    <my-header slot="header"
               :project-sub-text="dialogTitle"  :project-sub-title="'业主代表检查'"    :project-name="project.project_name"  ></my-header>
    <div slot="body">
      <widgets title="标段名称" :value="project.sction_name" icon="section"></widgets>
      <widgets title="合同工期" :value="project.contract_time" icon="contract"></widgets>
      <widgets title="施工单位" :value="project.construction_organeation" icon="organization"></widgets>
      <widgets title="施工项目负责人" :value="project.project_manager" icon="responsibility"></widgets>
      <widgets title="监理单位" :value="project.jianli_company" icon="supervisor"></widgets>
      <widgets title="监理项目负责人" :value="project.jianli_name" icon="responsibility"></widgets>
      <div style="width: 100%;display: block;height: 50px;float: left;clear: both;line-height: 50px;font-size: 20px;margin-bottom:15px;">施工单位进度图</div>
      <chart :read-only="true" :project="chartProjectData"></chart>
      <div style="width: 100%;display: block;height: 50px;float: left;clear: both;line-height: 50px;font-size: 20px;margin-bottom:15px;">业主代表指定检查进度计划图</div>
      <chart :editable="true" :read-only="true" :is-edit-check="true" :check-data.sync="checkData" :project="chartProjectData"></chart>
      <upload lbl-text="上传附件" :exsits="project.file"></upload>
    </div>
  </my-dialog>
</template>

<script>

  import Dialog from "@/components/Dialog";
  import Header from "@/components/Dialog/Header";
  import Widgets from "@/components/Dialog/Widgets";
  import Chart from "@/components/Dialog/Chart";
  import Upload from "@/components/Dialog/Load";
  import  { mapState } from "vuex";

  export default{
    components:{
      'MyDialog':Dialog,
      'MyHeader':Header,
      Widgets,
      Chart,
      Upload
    },
    computed:mapState({
      user:state => state.user.userData
    }),
    data(){
      return {
        dialogTitle:"",
        chartProjectData:[],
        project:{
          project_name:"",
          sction_name:"",
          contract_time:"",
          construction_organeation:"",
          jianli_company:"",
          jianli_name:"",
          project_manager:"",
          file:[]
        },
        checkData:[],
        showLoading:false,
        progress_id:-1,
        tempProject:null
      };
    },
    created(){

      /*setTimeout(()=>{



        this.project=[
          {
            projectName:"项目1",
            items:[{
              subName:"子选项1",
              startDate:'2015-11-18',
              endDate:'2015-12-15'
            },{
              subName:"子选项2",
              startDate:'2015-11-1',
              endDate:'2016-2-29'
            }]
          },
          {
            projectName:"项目3",
            items:[{
              subName:"子选项3",
              startDate:'2017-4-18',
              endDate:'2017-6-2'
            },{
              subName:"子选项4",
              startDate:'2016-2-28',
              endDate:'2016-3-28'
            }]
          },{
            projectName:"项目5",
            items:[{
              subName:"子选项6",
              startDate:'2015-11-18',
              endDate:'2015-12-15'
            },{
              subName:"子选项7",
              startDate:'2015-11-1',
              endDate:'2016-2-29'
            }]
          },
          {
            projectName:"项目6",
            items:[{
              subName:"子选项8",
              startDate:'2013-9-6',
              endDate:'2017-6-15'
            },{
              subName:'子选项9',
              startDate:'2016-2-28',
              endDate:'2016-3-28'
            }]
          }
        ];

        this.checkData=[{
          projectName:"项目1",
          projectSubName:"子选项1",
          checkTime:"2015-11-30"
        },{
          projectName:"项目6",
          projectSubName:"子选项8",
          checkTime:"2013-10-1"
        }];


      });*/


      this.projectId=this.$route.params.projectId;

      var taskId=this.$route.params.taskId;

      this.$store.dispatch('getProgressPlan',{
        'token':this.user.token,
        'pId':this.projectId,
        'taskId':taskId
      }).then((res) => {

        this.progress_id=res.data.id;

        this.dialogTitle=res.data.name;

        var project = res.data.project;

        var branch_project=res.data.branch_project;

        this.tempProject = JSON.parse(JSON.stringify(branch_project));

        for (var key in this.project){

          if (this.project.hasOwnProperty(key)&&key!='file'){

            this.project[key]=project[key];

          }

          if (key=='file'){

            for (var i=0;i<project.file.length;i++){
              this.project.file.push(project['file'][i]);
            }

          }
        }

        for(var i=0;i<branch_project.length;i++){

          var tempObj = {
            projectName:"",
            items:[]
          };

          var primary=branch_project[i];

          tempObj.projectName=primary.branch_project_name;

          for (var j=0;j<primary.branch_son_project.length;j++){


            var tempSubObj={
              subName:"",
              startDate:'',
              endDate:''
            };

            var secondary=primary.branch_son_project[j];

            tempSubObj.subName=secondary.branch_son_project_name;
            tempSubObj.startDate=secondary.construction_begin_at;
            tempSubObj.endDate=secondary.construction_end_at;

            tempObj.items.push(tempSubObj);

          }

          this.chartProjectData.push(tempObj);

        }

      },message => {

        this.$message('错误，请求数据失败,错误信息:'+message);

        //this.$router.go(-1);

      }).catch(err=>{

        this.$message('系统错误，请求数据失败，错误信息:'+err);

        //this.$router.go(-1);

      });

    },
    methods:{
      submitHandle(){

        var data=this.getChartData();

        var tempFile=[];

        this.project.file.forEach(item =>{

          tempFile.push({id:item.id});

        });

        console.log(JSON.stringify( tempFile));

       this.$confirm('即将提交业主代表检查计划, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          this.$store.dispatch('addSendCheckPlan',{
            token:this.user.token,
            task_id:this.$route.params.taskId,
            progress_plan_id:this.progress_id,
            users_id:this.user.id,
            project_id:this.$route.params.projectId,
            file:tempFile,
            branch_son_project:data
          }).then(res =>{

            if(res.statusText=="OK"&&res.data=="ok"){

              this.$message('提交检查计划成功!');

              this.$router.go(-1);

            }
            else {

              this.$message("提交失败，提交检查计划失败，失败信息："+JSON.stringify(res));

            }
          },res => {

            this.$message("提交错误，提交检查计划失败，错误信息："+JSON.stringify(res));

          }).catch(err =>{

            this.$message("系统错误，提交检查计划失败，错误信息："+JSON.stringify(err));

          });

        });

      },
      getChartData(){

        var checkData=JSON.parse(JSON.stringify(this.checkData));

        var branch_son_project=[];

        for(var i=0;i<checkData.length;i++){

          var projectName=checkData[i].projectName;

          var projectSubName=checkData[i].projectSubName;

          var checkTime=checkData[i].checkTime.replace(/\//g,'-');

          for (var a=0;a<this.tempProject.length;a++){

            for (var b=0;b<this.tempProject[a].branch_son_project.length;b++){

              if (this.tempProject[a].branch_project_name==projectName&&this.tempProject[a].
                  branch_son_project[b].branch_son_project_name==projectSubName){

                var id=this.tempProject[a].branch_son_project[b].id;

                var target={
                  id,
                  branch_son_project_name:projectSubName,
                  check_plan:[
                    {
                      plan_at:checkTime
                    }
                  ]
                }
                branch_son_project.push(target);
              }
            }
          }
        }

        return branch_son_project;

      }
    }
  }
</script>
